#!/usr/bin/python

import os;
import string;
import gdb;

def get_tvariables(name):
	os.remove("./rwpy.gdb.log");
	gdb.execute("set logging on");
	gdb.execute("info tvariables");
	gdb.execute("set logging off");
	fp = open("./rwpy.gdb.log");
	tv = fp.read();
	fp.close();
	if tv.find(name) < 0:
		raise (TypeError, 'Cannot find out name');
	tv = tv[tv.find(name) + len(name):];
	tv = tv[tv.find("0") + len("0"):];
	tv = tv[:tv.find("\n") + len("\n")];
	return string.atol(tv);

def tfind_entry(num):
	if num < 0:
		tid = gdb.execute("tfind", False, True);
	else:
		tid = gdb.execute("tfind "+str(num), False, True);
	if tid.find(", tracepoint ") < 0:
		raise (TypeError, 'Cannot find tid');
	tid = tid[tid.find(", tracepoint ") + len(", tracepoint "):];
	return int(tid);

#get_tvariables need this
gdb.execute("set logging file ./rwpy.gdb.log", True, True);
gdb.execute("set logging overwrite on", True, True);
gdb.execute("set logging redirect on", False, True);
gdb.execute("set logging off", True, True);
gdb.execute("set height 0", True, True);
gdb.execute("set width 0", True, True);
gdb.execute("set pagination off", True, True);

#set tracepoint id
vfs_read_begin = 1;
vfs_read_end = 2;
vfs_write_begin = 3;
vfs_write_end = 4;

#connect to the gdbframe_pipe
gdb.execute("target tfile /sys/kernel/debug/gtpframe_pipe");

#gdb.execute("target tfile ggg");

r_list = [];
read_clock = 0L;
read_num = 0L;
w_list = [];


while 1:
	#try:
		tid = tfind_entry(-1);

		if tid == vfs_read_begin:
			entry = {};
			entry["fid"] = gdb.selected_frame();
			entry["clock"] = get_tvariables("$clock");
			entry["pid"] = str(get_tvariables("$current_task"));
			entry["pid"] = str(gdb.parse_and_eval("((struct task_struct *)"+entry["pid"]+")->pid"));
			entry["pid"] = string.atol(entry["pid"]);
			r_list.append(entry);
		elif tid == vfs_read_end:
			fid = gdb.selected_frame();
			pid = str(get_tvariables("$current_task"));
			pid = str(gdb.parse_and_eval("((struct task_struct *)"+pid+")->pid"));
			pid = string.atol(pid);
			find_out = 0;
			for entry in r_list:
				if entry["pid"] == pid and entry["fid"] == fid:
					find_out = 1;
					break;
			if not find_out:
				print("Drop one vfs_read_end entry because cannot find vfs_read_begin for it.");
				continue;
			read_clock += get_tvariables("$clock") - entry["clock"];
			read_num += 1;
			print read_num, read_clock;
			r_list.remove(entry);

		gdb.execute("tfind", False, True);
	#except:
		#print("Something wrong, drop one entry.");
		#continue;


#i = 0;


#while 1:
	#gdb.execute("tfind 0");
	


	#filename = str(i)+".gtp";
	#i += 1;
	#if not os.path.exists(filename):
		#break;

	#gdb.execute("target tfile "+filename);

	#while 1:
		#ret = gdb.execute("tfind", False, True);
		#if ret.find("Found trace frame") < 0:
			#break;
		#try:
			#ret = gdb.execute("p h->action", False, True);
		#except:
			#print("Something wrong, drop one entry.");
			#continue;
		#ret = ret[ret.find(" = ") + 3:len(ret) - 1];
		#if ret in softirq_list:
			#softirq_list[ret] += 1;
		#else:
			#softirq_list[ret] = 1L;

#for key in softirq_list:
	#print(key, softirq_list[key]);


